import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';
import { Exclude } from 'class-transformer';
import { UserRole } from './user-role.entity';

@Entity('users')
export class User {
  @PrimaryGeneratedColumn({ type: 'bigint' })
  id: number;

  @Column({ type: 'varchar', length: 50, unique: true, comment: '用户名' })
  username: string;

  @Column({ type: 'varchar', length: 255, comment: '密码' })
  @Exclude()
  password: string;

  @Column({ type: 'varchar', length: 100, unique: true, comment: '邮箱' })
  email: string;

  @Column({ type: 'varchar', length: 50, nullable: true, comment: '昵称' })
  nickname?: string;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '头像' })
  avatar?: string;

  @Column({ type: 'tinyint', default: 1, comment: '状态：1-启用，0-禁用' })
  status: number;

  @Column({ type: 'varchar', length: 15, nullable: true, comment: '手机号' })
  phone?: string;

  @Column({ type: 'datetime', nullable: true, comment: '最后登录时间' })
  lastLoginAt?: Date;

  @Column({ type: 'varchar', length: 45, nullable: true, comment: '最后登录IP' })
  lastLoginIp?: string;

  @CreateDateColumn({ comment: '创建时间' })
  createTime: Date;

  @UpdateDateColumn({ comment: '更新时间' })
  updateTime: Date;

  @OneToMany(() => UserRole, userRole => userRole.user)
  userRoles: UserRole[];
}